package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    CartService cartService;

    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo add(@RequestBody Cart cart){
        Cart cartList = cartService.add(cart);
        return ResultInfo.success(cartList);
    }

    //得到购物车中数据
    @GetMapping("/cart/list")
    public ResultInfo list(){
        List<Cart> list = cartService.list();
        return ResultInfo.success(list);
    }

    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo delete(){
        cartService.delete();
        return ResultInfo.success();
    }

    //删除购物车部分数据
    @PostMapping("/cart/sub")
    public ResultInfo deleteByDishId(@RequestBody Cart cart){
        Cart newCart = cartService.deleteByDishId(cart);
        return ResultInfo.success(newCart);
    }
}
